package com.syty.demo.handler;

import com.syty.demo.exception.MyException;
import com.syty.demo.result.ExceptionResultSet;
import com.syty.demo.result.ResultCodes;
import com.syty.demo.result.ResultSet;
import com.syty.demo.util.LogUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 测试
 * 统一异常处理
 */
@Api(tags = "异常处理器",value = "对自定义异常进行处理")
@RestControllerAdvice
public class TestExceptionHandler {

    @ExceptionHandler(value = MyException.class)
    public ResultSet handlerException(MyException exception){
//        ModelAndView modelAndView = new ModelAndView();
        ExceptionResultSet resultSet = exception.getExceptionResultSet();
        switch (resultSet.getErrorCode()){
            case 1001:
                LogUtils.write("测试代码" + resultSet.getMessage());
                break;
            case 1002:
                LogUtils.write("[登录]--用户信息不能为空" + "message：" + resultSet.getMessage());
//                modelAndView.addObject("[登录]--用户信息不能为空",resultSet.getMessage());
                break;
            case 1003:
                LogUtils.write("[注册]--注册信息不能为空" + "message：" + resultSet.getMessage());
//                modelAndView.addObject("注册信息不能为空",resultSet.getMessage());
                break;
            case 1004:
                LogUtils.write("[注册]--用户名已存在" + "message：" + resultSet.getMessage());
//                modelAndView.addObject("[注册]--用户名已存在",resultSet.getMessage());
                break;
            case 1005:
                LogUtils.write("[注册]--邮箱格式错误" + "message：" + resultSet.getMessage());
//                modelAndView.addObject("[注册]--邮箱格式错误",resultSet.getMessage());
                break;
            case 1006:
                LogUtils.write("[注册]--登录信息保存失败" + "message：" + resultSet.getMessage());
//                modelAndView.addObject("[注册]--信息保存失败",resultSet.getMessage());
                break;
            case 1007:
                LogUtils.write("[注册]--添加用户失败" + "message：" + resultSet.getMessage());
                break;
            case 1008:
                LogUtils.write("[注册]--用户信息保存失败" + "message：" + resultSet.getMessage());
                break;
            case 1009:
                LogUtils.write("[重置]--用户不存在" + "message：" + resultSet.getMessage());
                return new ResultSet(ResultCodes.REMAKE_SUBMIT_FAIL,false,"重置密码失败",null);
            case 1010:
                LogUtils.write("[重置]--邮箱不匹配" + "message：" + resultSet.getMessage());
                return new ResultSet(ResultCodes.REMAKE_SUBMIT_FAIL,false,"重置密码失败",null);
            case 1011:
                LogUtils.write("[重置]--信息为空" + "message：" + resultSet.getMessage());
                return new ResultSet(ResultCodes.REMAKE_SUBMIT_FAIL,false,"重置密码失败",null);
            case 1012:
                LogUtils.write("[重置]--重置失败" + "message：" + resultSet.getMessage());
                return new ResultSet(ResultCodes.REMAKE_SUBMIT_FAIL,false,"重置密码失败",null);
            default:
                LogUtils.write("ErrorCode 异常！");
        }

        return null;
    }
}
